import 'package:boss/models/package_group.dart';
import 'package:boss/provider/package_group_provider.dart';
import 'package:boss/ui/page/packageGroup/page/packageGroupDetail/package_group_detail_page.dart';
import 'package:boss/ui/page/packageGroup/page/packageGroupDetail/provider/package_group_detail_provider.dart';
import 'package:boss/ui/widget/button/fixed_bottom_btn.dart';
import 'package:boss/ui/widget/button/fixed_bottom_double_btn.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class PackageGroupDetailFooter extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer<PackageGroupDetailProvider>(
      builder: (context, provider, child) {
        return provider.isEdit
            ? FixedBottomDoubleBtn(
                first: '复制',
                onFirst: () {
                  var copy = PackageGroup.fromJson(provider.packageGroup.toJson());
                  copy.id = null;
                  Navigator.popAndPushNamed(context, PackageGroupDetailPage.pageName, arguments: {
                    'packageGroup': copy,
                  });
                },
                second: '保存',
                onSecond: () async {
                  var packageGroupProvider = Provider.of<PackageGroupProvider>(
                    context,
                    listen: false,
                  );
                  if (await packageGroupProvider.resetPackageGroup(provider.packageGroup)) {
                    Navigator.pop(context);
                  }
                },
              )
            : FixedBottomBtn(
                title: '保存',
                onTap: () async {
                  var packageGroupProvider = Provider.of<PackageGroupProvider>(
                    context,
                    listen: false,
                  );
                  var packageGroupDetailProvider = Provider.of<PackageGroupDetailProvider>(
                    context,
                    listen: false,
                  );
                  var flag = await packageGroupProvider.plusPackageGroup(packageGroupDetailProvider.packageGroup);
                  if (flag) {
                    Navigator.pop(context, packageGroupDetailProvider.packageGroup);
                  }
                },
              );
      },
    );
  }
}
